#include "dialog_login.h"
#include "ui_dialog_login.h"
#include <QMessageBox>
#include <QSettings>
#include <QDebug>
Dialog_login::Dialog_login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_login)
{
    ui->setupUi(this);
//    this->setWindowFlags(Qt::CustomizeWindowHint);
}

Dialog_login::~Dialog_login()
{
    delete ui;
}

QString Dialog_login::readPassword(const QString& username)
{
    QSettings settings("./config/account.ini", QSettings::IniFormat);
    settings.beginGroup(username);
    QString password = settings.value("password").toString();
    settings.endGroup();
    return password;
}

void Dialog_login::on_btn_login_clicked()
{
    QString InputPwd = ui->lineEdit_password->text().trimmed();
    QString userName = ui->comboBox_user->currentText().trimmed();

    if(ui->comboBox_user->count() == 0)
    {
        qDebug() << "没有用户，请先注册...";
        return;
    }

    if(userName.isEmpty())
    {
        qDebug() << "用户名不能为空，请输入用户名...";
        return;
    }

    if(InputPwd.isEmpty())
    {
        qDebug() << "密码不能为空，请输入密码...";
        return;
    }


    QString pwd = readPassword(ui->comboBox_user->currentText());
    if(InputPwd.compare(pwd)==0)
        accept();
    else
    {
        QMessageBox::warning(this,"提示","密码错误，不允许操作");
    }
}

void Dialog_login::closeEvent(QCloseEvent *event)
{
    // 显示警告框
    QMessageBox messageBox;
    messageBox.setWindowTitle("退出");
    messageBox.setInformativeText("确定退出吗？");
    messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);

    // 将Yes按钮改为中文“是”
    messageBox.setButtonText(QMessageBox::Yes, QString::fromUtf8("是"));
    // 将No按钮改为中文“否”
    messageBox.setButtonText(QMessageBox::No, QString::fromUtf8("否"));

    if (messageBox.exec() == QMessageBox::Yes) {
        // 用户选择了“是”
        // 执行相关操作
//        event->accept();
        reject();
    } else {
        // 用户选择了“否”
        // 执行相关操作
        event->ignore();
    }
}
